package com.github.ilioili.justdoit.model.dao;

import android.database.Cursor;
import android.os.Handler;
import android.os.Looper;

import com.github.ilioili.justdoit.model.pojo.EventRecord;
import com.github.ilioili.justdoit.util.sql.DbUtil;
import com.github.ilioili.justdoit.util.sql.SqlCallback;
import com.github.ilioili.justdoit.util.sql.SqlUtil;

import java.util.List;

/**
 * Created by hantuo on 16/5/13.
 */
public class RecordDao {
    public static void getList(final int year, final int month, final long habbitId, final SqlCallback<List<EventRecord>> sqlCallback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Cursor cursor = DbUtil.getDb().rawQuery("select * from " + SqlUtil.getTableName(EventRecord.class) + " where _eventId = " + habbitId + " and _month = " + month + " and _year = " + year, null);
                final List<EventRecord> list = DbUtil.getItemList(EventRecord.class, cursor);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        sqlCallback.onSucess(list);
                    }
                });
            }
        }).start();
    }

    public static void getListOfDay(final int year, final int month, final int day, final SqlCallback<List<EventRecord>> sqlCallback) {
//        Cursor cursor = DbUtil.getDb().rawQuery("select * from " + SqlUtil.getTableName(EventRecord.class) + " where _day = " + day + " and _month = " + month + " and _year = " + year, null);
//        return DbUtil.getItemList(EventRecord.class, cursor);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Cursor cursor = DbUtil.getDb().rawQuery("select * from " + SqlUtil.getTableName(EventRecord.class) + " where _day = " + day + " and _month = " + month + " and _year = " + year, null);
                final List<EventRecord> list = DbUtil.getItemList(EventRecord.class, cursor);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        sqlCallback.onSucess(list);
                    }
                });
            }
        }).start();
    }


    public static void getList(final long habbitId,final SqlCallback<List<EventRecord>> sqlCallback) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                String sql = SqlUtil.getSqlForSelectWithCondition(EventRecord.class, "_eventId = " + habbitId + " order by _id asc");
                Cursor cursor = DbUtil.getDb().rawQuery(sql, null);
                final List<EventRecord> list = DbUtil.getItemList(EventRecord.class, cursor);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        sqlCallback.onSucess(list);
                    }
                });
            }
        }).start();
    }

    public static void replace(EventRecord eventRecord) {
        DbUtil.replace(eventRecord);
    }

    public static void delete(EventRecord eventRecord) {
        DbUtil.delete(eventRecord);
    }

}
